<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: Logout.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\user;

use app\api\model\dao\UserAccount;
use app\api\model\dao\UserAuths;
use think\Exception;
use app\library\Error;
use app\library\PageBase;
use think\Session;
use think\Validate;
use think\Cache;
use think\Log;

class Logout extends PageBase
{

    private $objUserAccount;
    private $objUserAuths;

    public function __construct()
    {
        parent::__construct();

        $this->objUserAccount = new UserAccount();
        $this->objUserAuths = new UserAuths();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'token' => 'require',
        ];
        $msg = [
            'token.require' => 'token必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            Log::alert(__METHOD__ . ' param: ' . json_encode($param));
            return $this->getRes(Error::ERR_PARAM);
        }

        // 删除sessionid
        Cache::rm($param['token']);

        return $this->getRes();
    }
}
